package org.jberney.wvdexporter.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.jberney.wvdexporter.io.WvdEncodeProgressListener;
import org.eclipse.swt.widgets.ProgressBar;

public class ExportProgressShell extends Shell implements
		WvdEncodeProgressListener {

	private double ratio;
	private ProgressBar progressBar;
	private int progress;

	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			ExportProgressShell shell = new ExportProgressShell(display, 100);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ExportProgressShell(Display display, long numBytes) {
		super(display, SWT.TITLE | SWT.TOOL);
		this.ratio = new Double(Integer.MAX_VALUE) / numBytes;
		createContents();
	}

	protected void createContents() {

		setText("Export Progress");
		setSize(320, 50);

		this.progressBar = new ProgressBar(this, SWT.NONE);
		this.progressBar.setBounds(10, 10, 300, 14);
		this.progressBar.setMaximum(Integer.MAX_VALUE);
		this.progressBar.setMinimum(0);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	protected void updateProgressBar() {
		this.progressBar.setSelection(this.progress);
	}

	public void incrementProgress(int increment) {
		this.progress += (int) Math.round(increment * this.ratio);
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				ExportProgressShell.this.updateProgressBar();
			}
		});
	}
}
